#include "Player.h"

Player::Player(): mOffset(1.6f), mLastAngle(0), mPlayerMove(0.0f,0.0f,0.0f), mPlayerRotate(0.0f,0.0f,0.0f),
				  mPowerLevel(0), mHealth(100.0f),mPowerupAvailable(false),
				  mCurrentPowerUpAmount(20)
{
}

Player::~Player(){}

//-- Set the local scale matrix of the player
void Player::SetLocalScale(abfw::Vector3 scale)
{
	mScale.Scale(scale);
}

abfw::Matrix44 Player::GetLocalScale()
{
	return mScale;
}
abfw::Vector3 Player::GetPlayerRotate()
{
	return mPlayerRotate;
}
abfw::Vector3 Player::GetPlayerMove()
{
	return mPlayerMove;
}

float Player::GetHealth()
{
	return mHealth;
}

//-- Add the given value to the players health
void Player::AddHealth(float value)
{
	mHealth += value;
}

//-- Update the player
void Player::Update(abfw::SonyControllerInputManager* input, abfw::Matrix44 marker)
{
	//-- Check if a turret can be created
	CheckPowerLevel();

	//-- Get the PS Vita input for movement
	mPlayerMove.x += input->left_stick_x_axis() / 400;
	mPlayerMove.y -= input->left_stick_y_axis() / 400;
	mPlayerMove.z = 0.02;
	//-- Get the right sticks angle for rotation
	mPlayerRotate.z = CalculateRotationAngle(input);

	//-- Set transformation matrices
	SetLocalScale(abfw::Vector3(0.002195f, 0.002195f, 0.002195f));
	SetLocalRotationX(1.6f);	
	SetLocalRotationY(0.0f);
	SetLocalRotationZ(mPlayerRotate.z - 3.2f);
	SetLocalTranslation(mPlayerMove);

	//-- Set final transform
	SetTransform(GetLocalScale() * GetLocalRotation()  * GetLocalTranslation() * marker);
}

void Player::CheckPowerLevel()
{
	//-- check power level status
	if (mPowerLevel >= 100)
	{
		mPowerupAvailable = true;
	}
}
void Player::Reset()
{
	//-- Reset to origin
	mPlayerMove.x = 0;
	mPlayerMove.y = 0;
	SetTransform(GetOrigin());
}

float Player::CalculateRotationAngle(abfw::SonyControllerInputManager* input)
{
	//-- Get the rotation angle from the right stick
	float value;
	value = (float)atan2(input->right_stick_x_axis(), input->right_stick_y_axis()) - mOffset;

	//-- Store the last angle for when the stick is released
	if (input->right_stick_x_axis() != 0 && 
			input->right_stick_y_axis() != 0)
	{
		mLastAngle = value;
	}
	else
	{
		value = mLastAngle;
	}

	return value;
}

void Player::Draw(abfw::Renderer3D* render3D)
{
	render3D->DrawMesh(GetMeshInstance());
	
}

void Player::DrawHud(abfw::SpriteRenderer* render2D)
{

}


